package com.woniuxy.cloud.areaassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.areaApi.dto.AreaDto;
import com.woniuxy.cloud.areaApi.AreaQueryAPI;
import com.woniuxy.cloud.areaassembly.entity.SysArea;
import com.woniuxy.cloud.areaassembly.exception.AreaException;
import com.woniuxy.cloud.areaassembly.service.SysAreaService;
import com.woniuxy.cloud.areaassembly.util.ValidatedUtil;
import com.woniuxy.cloud.areaassembly.util.WoniuBeanUtils;
import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.areaApi.param.initiationAreaParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周川雄
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/sys-area")
public class SysAreaController implements AreaQueryAPI {
    @Resource
    private SysAreaService areaService;


    @Override
    public AreaDto getAreaById(Integer areaId) {
        SysArea area = areaService.getById(areaId);
        if (StringUtils.isEmpty(area)) throw new AreaException("区域id错误，无区域存在", 10503);
        return WoniuBeanUtils.copyObject(area, AreaDto.class);
    }

    @ApiOperation("查询指定区域")
    @Override
    public Result listArea(@RequestParam("areaId") Integer areaId) {
        List<AreaDto> dtos = areaService.listArea(areaId, null);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dtos);
    }

    @ApiOperation("查询指定可用区域")
    @Override
    public Result listInitiationArea(@RequestParam("areaId") Integer areaId) {
        List<AreaDto> dtos = areaService.listArea(areaId, new QueryWrapper<SysArea>().eq("is_initiation", 1));

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dtos);
    }


    @ApiOperation("开启区域权限")
    @Override
    public Result initiationArea(@RequestBody @Validated initiationAreaParam param) {
        if (areaService.initiationArea(param)) {
            return Result.newInstance().setCode(200).setMsg("开启成功");
        } else {
            throw new AreaException("开启权限失败", 500);
        }

    }


}

